package com.yidu.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.common.Pages;
import com.yidu.common.Tools;
import com.yidu.domain.ListenQualityTesting;
import com.yidu.service.ListenQualityTestingService;

/**
 * @title 质检Controller类
 * @author 江宇
 * @dateTime 2019年1月14日
 */
@Controller
@RequestMapping("/listenQualityTesting")
public class ListenQualityTestingController {
	@Resource
	ListenQualityTestingService listenQualityTestingService;
	
	/**
     * @title 查询质检
     * @param Map
     * @return List
     * @author 江宇
     * @dateTime 2018年12月23日
     * @versions 1.0
     */
	@RequestMapping("findQualityTesting")
	@ResponseBody
	public Map<String, Object> findQualityTesting(ListenQualityTesting qt){
		Map<String, Object> map=new HashMap<>();
		
		//如果质检类型不为空赋值
		if(qt.getQtType()!=null) {
			map.put("qtType", qt.getQtType());
		}
		
		//如果公司ID不为空赋值
		if(!Tools.isEmpty(qt.getCompanyId())) {
			map.put("companyId", qt.getCompanyId());
		}
		//如果开始时间不为空赋值
		if(!Tools.isEmpty(qt.getStartTime())) {
			map.put("startTime", qt.getStartTime()+" 00:00:00");
		}
		
		//如果结束时间不为空赋值
		if(!Tools.isEmpty(qt.getEndTime())) {
			map.put("endTime", qt.getEndTime()+" 23:59:59");
		}
		
		//如果前台传来的页数和行数不为空赋值
		if(qt.getPage()!=null && qt.getLimit()!=null) {
			Pages page=new Pages();
			page.setCurPage(qt.getPage());
			page.setMaxResult(qt.getLimit());
			
			map.put("firstRows", page.getFirstRows());
			map.put("maxResult", page.getMaxResult());
		}
		
		
		//调用查询质检的方法
		List<ListenQualityTesting> list=listenQualityTestingService.findQualityTesting(map);
		//创建质检List集合
		List<ListenQualityTesting> qtList=new ArrayList<>();
		//循环查询的质检的List集合
		for (ListenQualityTesting listenQualityTesting : list) {
			//创建质检类对象
			ListenQualityTesting qt2=new ListenQualityTesting();
			//赋值
			BeanUtils.copyProperties(listenQualityTesting, qt2);
			//时间转换
			qt2.setStringQtTime(Tools.SDF_TIME.format(listenQualityTesting.getQtTime()));
			qtList.add(qt2);
		}
		//调用查询质检行数的方法
		int rows=listenQualityTestingService.findQualityTestingCount(map);
		
		Map<String, Object> layuiMap=new HashMap<>();
		
		layuiMap.put("code", 0);
		layuiMap.put("msg", "");
		layuiMap.put("count",rows);
		//将list放入map中
		layuiMap.put("data", qtList);
		return layuiMap;
	}
	
	
	 /**
     * @title 增加质检 
     * @param ListenQualityTesting
     * @return int
     * @author 江宇
     * @dateTime 2018年12月23日
     * @versions 1.0
     */
	@RequestMapping("addQualityTesting")
	@ResponseBody
	public int addQualityTesting(String submitString,HttpSession session){
		return listenQualityTestingService.addQualityTesting(submitString,session);
	}
}

